package org.lex.swing.plaf;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SpinnerDateModel;

import org.lex.swing.Swings;
import org.lex.swing.table.TableHelper;
import org.lex.swing.widgets.MemoryBar;
import org.lex.swing.widgets.NetworkInterfaceTree;
import org.lex.utils.SystemProperties;


public class WidgetsSample extends JPanel {
	private JScrollPane treeScrollPane = null;
	private JScrollPane tableScrollPane = null;
	private JTabbedPane tabPane = null;
	private JTextField textField = null;
	private JTextField fixedTextField = null;
	private JComboBox comboBox = null;
	private JComboBox fixedComboBox = null;
	private JButton button = null;
	private JToggleButton toggleButton = null;
	private NetworkInterfaceTree tree = null;
	private JTable table = null;
	private JPanel buttonPane = null;
	private JPanel textPane = null;
	private JLabel label = null;
	private JRadioButton radioButton = null;
	private JCheckBox checkBox = null;
	private JProgressBar progressBar = null;
	private JSlider slider = null;
	private JSpinner spinner = null;

	public static void main(String[] args) {
		Swings.show(new WidgetsSample(), false);
	}

	public WidgetsSample() {
		super();
		initialize();
	}

	private void initialize() {
		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setVgap(6);
		this.setLayout(borderLayout);
		this.add(getCenterPane(), BorderLayout.CENTER);
		this.add(getSouthPane(), BorderLayout.SOUTH);
	}

	private JScrollPane getTreeScrollPane() {
		if (treeScrollPane == null) {
			treeScrollPane = new JScrollPane();
			treeScrollPane.setViewportView(getTree());
		}
		return treeScrollPane;
	}

	private JScrollPane getTableScrollPane() {
		if (tableScrollPane == null) {
			tableScrollPane = new JScrollPane();
			tableScrollPane.setViewportView(getTable());
		}
		return tableScrollPane;
	}

	private JTabbedPane getTabPane() {
		if (tabPane == null) {
			tabPane = new JTabbedPane();
			tabPane.addTab("Button", null, getButtonPane(), null);
			tabPane.addTab("Text", null, getTextPane(), null);
		}
		return tabPane;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setText("JTextField");
		}
		return textField;
	}

	private JTextField getFixedTextField() {
		if (fixedTextField == null) {
			fixedTextField = new JTextField();
			fixedTextField.setEditable(false);
			fixedTextField.setText("Not Editable");
		}
		return fixedTextField;
	}

	private static final String[] LANGS = { "Java", "C++", "Python" };
	private JPanel centerPane = null;
	private JPanel southPane = null;
	private JPanel gluePanel = null;
	private JLabel fixedLabel = null;
	private JPanel gluePanel2 = null;

	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox(LANGS);
			comboBox.setEditable(true);
			comboBox.setSelectedIndex(0);
		}
		return comboBox;
	}

	private JComboBox getFixedComboBox() {
		if (fixedComboBox == null) {
			fixedComboBox = new JComboBox(LANGS);
			fixedComboBox.setEditable(false);
			fixedComboBox.setSelectedIndex(0);
		}
		return fixedComboBox;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton();
			button.setText("Button");
		}
		return button;
	}

	private JToggleButton getToggleButton() {
		if (toggleButton == null) {
			toggleButton = new JToggleButton();
			toggleButton.setText("Toggle");
		}
		return toggleButton;
	}

	private NetworkInterfaceTree getTree() {
		if (tree == null) {
			tree = new NetworkInterfaceTree();
		}
		return tree;
	}

	private JTable getTable() {
		if (table == null) {
			Map<String, String> map = new HashMap<String, String>();
			put(map, SystemProperties.Key.JAVA_VENDOR);
			put(map, SystemProperties.Key.JAVA_VM_INFO);
			put(map, SystemProperties.Key.JAVA_VM_NAME);
			put(map, SystemProperties.Key.JAVA_VM_VERSION);
			put(map, SystemProperties.Key.JAVA_RUNTIME_VERSION);
			put(map, SystemProperties.Key.OS_NAME);
			put(map, SystemProperties.Key.OS_VERSION);
			put(map, SystemProperties.Key.OS_VERSION);
			table = new JTable(TableHelper.createModel(map));
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			table.getColumnModel().getColumn(0).setPreferredWidth(120);
			table.getColumnModel().getColumn(1).setPreferredWidth(150);
			table.setAutoCreateRowSorter(true);
			table.getRowSorter().toggleSortOrder(0);
		}
		return table;
	}

	private void put(Map<String, String> map, String key) {
		String value = System.getProperty(key);
		if (null != value)
			map.put(key, value);
	}

	private JPanel getButtonPane() {
		if (buttonPane == null) {
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 0;
			gridBagConstraints14.fill = GridBagConstraints.BOTH;
			gridBagConstraints14.weightx = 1.0;
			gridBagConstraints14.weighty = 1.0;
			gridBagConstraints14.gridy = 4;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.insets = new Insets(6, 6, 0, 0);
			gridBagConstraints13.gridx = 0;
			gridBagConstraints13.gridy = 3;
			gridBagConstraints13.anchor = GridBagConstraints.WEST;
			gridBagConstraints13.gridwidth = 1;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.insets = new Insets(6, 6, 0, 0);
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.gridy = 2;
			gridBagConstraints12.anchor = GridBagConstraints.WEST;
			gridBagConstraints12.fill = GridBagConstraints.NONE;
			gridBagConstraints12.gridwidth = 1;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.insets = new Insets(6, 6, 0, 0);
			gridBagConstraints9.gridy = 1;
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.gridx = 0;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.insets = new Insets(6, 6, 0, 0);
			gridBagConstraints8.gridy = 0;
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints8.gridx = 0;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.gridx = -1;
			gridBagConstraints11.gridy = -1;
			gridBagConstraints11.weightx = 0.0;
			gridBagConstraints11.insets = new Insets(12, 6, 0, 0);
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints10.gridx = -1;
			gridBagConstraints10.gridy = -1;
			gridBagConstraints10.weightx = 0.0;
			gridBagConstraints10.insets = new Insets(12, 12, 0, 0);
			label = new JLabel();
			label.setText("JLabel");
			buttonPane = new JPanel();
			buttonPane.setLayout(new GridBagLayout());
			buttonPane.add(getRadioButton(), gridBagConstraints8);
			buttonPane.add(getCheckBox(), gridBagConstraints9);
			buttonPane.add(getButton(), gridBagConstraints12);
			buttonPane.add(getToggleButton(), gridBagConstraints13);
			buttonPane.add(getGluePanel(), gridBagConstraints14);
		}
		return buttonPane;
	}

	private JPanel getTextPane() {
		if (textPane == null) {
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 0;
			gridBagConstraints16.fill = GridBagConstraints.BOTH;
			gridBagConstraints16.weightx = 1.0;
			gridBagConstraints16.weighty = 1.0;
			gridBagConstraints16.gridy = 8;
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.gridx = 0;
			gridBagConstraints15.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints15.anchor = GridBagConstraints.WEST;
			gridBagConstraints15.insets = new Insets(6, 6, 0, 0);
			gridBagConstraints15.gridy = 1;
			fixedLabel = new JLabel();
			fixedLabel.setText("Diabled JLabel");
			fixedLabel.setEnabled(false);
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridy = 7;
			gridBagConstraints7.fill = GridBagConstraints.NONE;
			gridBagConstraints7.insets = new Insets(6, 6, 0, 0);
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.insets = new Insets(6, 6, 0, 0);
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.NONE;
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 6;
			gridBagConstraints6.weightx = 0.0;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.insets = new Insets(6, 6, 0, 0);
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.NONE;
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 5;
			gridBagConstraints5.weightx = 0.0;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.insets = new Insets(6, 6, 0, 0);
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 4;
			gridBagConstraints4.weightx = 0.0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.insets = new Insets(6, 6, 0, 0);
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 3;
			gridBagConstraints3.weightx = 0.0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(6, 6, 0, 0);
			textPane = new JPanel();
			textPane.setLayout(new GridBagLayout());
			textPane.add(label, gridBagConstraints2);
			textPane.add(fixedLabel, gridBagConstraints15);
			textPane.add(getTextField(), gridBagConstraints3);
			textPane.add(getFixedTextField(), gridBagConstraints4);
			textPane.add(getComboBox(), gridBagConstraints5);
			textPane.add(getFixedComboBox(), gridBagConstraints6);
			textPane.add(getSpinner(), gridBagConstraints7);
			textPane.add(getGluePanel2(), gridBagConstraints16);
		}
		return textPane;
	}

	private JRadioButton getRadioButton() {
		if (radioButton == null) {
			radioButton = new JRadioButton();
			radioButton.setText("Radio");
		}
		return radioButton;
	}

	private JCheckBox getCheckBox() {
		if (checkBox == null) {
			checkBox = new JCheckBox();
			checkBox.setText("Check");
		}
		return checkBox;
	}

	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new MemoryBar();
			progressBar.setOrientation(JProgressBar.HORIZONTAL);
		}
		return progressBar;
	}

	private JSlider getSlider() {
		if (slider == null) {
			slider = new JSlider();
			slider.setOrientation(JSlider.HORIZONTAL);
			slider.setMinimum(0);
			slider.setMaximum(100);
			slider.setValue(75);
			slider.setMajorTickSpacing(10);
			slider.setMinorTickSpacing(5);
			slider.setPaintLabels(true);
			slider.setPaintTrack(true);
			slider.setPaintTicks(true);
		}
		return slider;
	}

	private JSpinner getSpinner() {
		if (spinner == null) {
			spinner = new JSpinner(new SpinnerDateModel());
		}
		return spinner;
	}

	private JPanel getCenterPane() {
		if (centerPane == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(1);
			gridLayout1.setHgap(6);
			gridLayout1.setColumns(3);
			centerPane = new JPanel();
			centerPane.setLayout(gridLayout1);
			centerPane.add(getTreeScrollPane(), null);
			centerPane.add(getTableScrollPane(), null);
			centerPane.add(getTabPane(), null);
		}
		return centerPane;
	}

	private JPanel getSouthPane() {
		if (southPane == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.ipadx = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.ipadx = 0;
			gridBagConstraints.ipady = 0;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.gridy = 0;
			southPane = new JPanel();
			southPane.setLayout(new GridBagLayout());
			southPane.add(getProgressBar(), gridBagConstraints);
			southPane.add(getSlider(), gridBagConstraints1);
		}
		return southPane;
	}

	private JPanel getGluePanel() {
		if (gluePanel == null) {
			gluePanel = new JPanel();
			gluePanel.setLayout(new GridBagLayout());
		}
		return gluePanel;
	}

	private JPanel getGluePanel2() {
		if (gluePanel2 == null) {
			gluePanel2 = new JPanel();
			gluePanel2.setLayout(new GridBagLayout());
		}
		return gluePanel2;
	}
} // @jve:decl-index=0:visual-constraint="10,10"
